package com.burgeon.backend.SHOP.service.Impl;

import com.burgeon.backend.SHOP.entity.AppTemplate;
import com.burgeon.backend.SHOP.entity.Wechat;
import com.burgeon.backend.SHOP.mappers.AppTemplateMapper;
import com.burgeon.backend.SHOP.mappers.WechatAppMapper;
import com.burgeon.backend.SHOP.service.SystemService;
import com.burgeon.backend.entity.AccessToken;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Service
public class SystemServiceImpl implements SystemService {
    @Autowired
    private AppTemplateMapper appTemplateMapper;
    @Autowired
    private WechatAppMapper wechatAppMapper;

    @Override
    public ArrayList<String> getTemplate(String token) {
        ArrayList<String> data = new ArrayList<String>();
        AppTemplate appTemplate = appTemplateMapper.GetTemplate(token);

        if (null != appTemplate.getTemplate1()&&!"".equals(appTemplate.getTemplate1())) {
            data.add(appTemplate.getTemplate1());
        }
        if (null != appTemplate.getTemplate2()&&!"".equals(appTemplate.getTemplate2())) {
            data.add(appTemplate.getTemplate2());
        }
        if (null != appTemplate.getTemplate3()&&!"".equals(appTemplate.getTemplate3())) {
            data.add(appTemplate.getTemplate3());
        }
        return data;
    }

    @Override
    public String GetAccesToken(String token) {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
        String dateString = sdf.format(now);
        AccessToken oldAccessToken = wechatAppMapper.getAccessToken(token);

        if (null != oldAccessToken && dateString.equals(oldAccessToken.getDate())) {
            return oldAccessToken.getAccessToken();
        }

        String accessToken;
        Wechat wechat = getWechat(token);
        String grantType = "client_credential";
        String appid = wechat.getAppid();
        String secret = wechat.getAppsecret();
        String url = "https://api.weixin.qq.com/cgi-bin/token?" +
                "grant_type=" + grantType +
                "&appid=" + appid +
                "&secret=" + secret;
        RestTemplate restTemplate = new RestTemplate();

        try {
            String str = restTemplate.getForObject(url, String.class);
            accessToken = new JSONObject(str).getString("access_token");
        } catch (JSONException e) {
            System.err.println("获取access_token失败");
            return null;
        }

        AccessToken data = new AccessToken();
        data.setAccessToken(accessToken);
        data.setToken(token);
        data.setDate(dateString);

        Integer row = wechatAppMapper.upDataAccessToken(data);
        if (row != 1) {
            //为acc添加accesstoken
            wechatAppMapper.addAccessToken(data);
        }
        return accessToken;
    }

    @Override
    public String AgainGetAccessToken(String token) {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
        String dateString = sdf.format(now);
        String accessToken;
        Wechat wechat = getWechat(token);
        String grantType = "client_credential";
        String appid = wechat.getAppid();
        String secret = wechat.getAppsecret();
        String url = "https://api.weixin.qq.com/cgi-bin/token?" +
                "grant_type=" + grantType +
                "&appid=" + appid +
                "&secret=" + secret;
        RestTemplate restTemplate = new RestTemplate();

        try {
            String str = restTemplate.getForObject(url, String.class);
            accessToken = new JSONObject(str).getString("access_token");
        } catch (JSONException e) {
            System.err.println("获取access_token失败");
            return null;
        }

        AccessToken data = new AccessToken();
        data.setAccessToken(accessToken);
        data.setToken(token);
        data.setDate(dateString);

        Integer row = wechatAppMapper.upDataAccessToken(data);
        if (row != 1) {
            //为acc添加accesstoken
            wechatAppMapper.addAccessToken(data);
        }
        return accessToken;
    }

    private Wechat getWechat(String token){
        return wechatAppMapper.getWechat(token);
    }
}
